package com.sywb.chuangyebao.contract;

import android.support.v7.widget.RecyclerView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.bean.Comment;
import com.sywb.chuangyebao.bean.VideoDataDetail;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.VideoDetailActivity;
import com.sywb.chuangyebao.view.dialog.ShareDialog;
import com.sywb.chuangyebao.view.dialog.VideoDetailIntroDialog;
import com.sywb.chuangyebao.widget.VideoDetailHeaderView;

import org.bining.footstone.utils.ToastUtils;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

public interface VideoDetailContract {

    interface View extends SupportCommentDetailContract.View {

        int getVideoId();

        void startPlay(String videoUrl, String previewPath);

        void setConsultState(boolean isConsult);
    }

    class Presenter extends SupportCommentDetailContract.Presenter<View> {

        protected VideoDetailHeaderView mHeaderView;

        private VideoDataDetail videoInfo;

        @Override
        public void onStart() {
            super.onStart();
            topicId = mView.getVideoId();
            topicType = Constants.TOPIC_TYPE_VIDEO;
            initData();
        }

        @Override
        public void initData() {
            super.initData();
            getVideo();
        }

        public void getVideo() {
            HttpUtils.getVideoInfo(topicId, new HttpCallback<VideoDataDetail>() {

                @Override
                public void onSuccess(VideoDataDetail model) {
                    if (model == null) {
                        return;
                    }

                    videoInfo = model;
                    mView.showCommentRelyDialog();
                    mHeaderView = new VideoDetailHeaderView(mActivity);
                    mHeaderView.setLayoutParams(new RecyclerView.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
                    mHeaderView.setVideoDetailHeaderViewListener(new VideoDetailHeaderView.VideoDetailHeaderViewListener() {
                        @Override
                        public void onUserClick() {
                            Utils.goUserIndex(mView, videoInfo.owner.uid);
                        }

                        @Override
                        public void onFollowClick() {
                            follow(videoInfo.owner.uid, !videoInfo.owner.is_follow);
                        }

                        @Override
                        public void onInfroClick() {
                            VideoDetailIntroDialog dialogFragment = VideoDetailIntroDialog.newInstance(mView.getDialogViewHeight(), videoInfo.desc);
                            dialogFragment.show(mView.getMyFragmentManager(), topicType);
                        }

                        @Override
                        public void onPraiseClick() {
                            agree(topicType, topicId, !videoInfo.is_thumb, -1);
                        }

                        @Override
                        public void onRecommenItemClick(int videoId) {
                            mView.advance(VideoDetailActivity.class, videoId);
                        }
                    });
                    mHeaderView.setDetail(videoInfo);
                    recyclerAdapter.setHeaderView(mHeaderView);// 自动调用 onStartLoadMore

                    mView.changeCommentCount(videoInfo.count.comment);
                    mView.setIsFavorite(videoInfo.is_favorite);

                    if (videoInfo.owner != null && (videoInfo.owner.getUserRole() == Constants.USER_TYPE_PROJECT || videoInfo.owner.uid == 1234567 || videoInfo.owner.uid == 0)) {
                        mView.setConsultState(true);
                    } else {
                        mView.setConsultState(false);
                    }

                    mView.startPlay(videoInfo.url, videoInfo.logo);

                    loadDetailFinish();
                    onStartRefresh();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }
            });
        }

        public void changeCommentCount(int postion, int count) {
            if (recyclerAdapter != null && recyclerAdapter.getDataCount() > postion) {
                Comment comment = recyclerAdapter.getDatas().get(postion);
                comment.reply_count = count;
                recyclerAdapter.notifyDataSetChanged();
            }
        }

        public void changeCommentThumb(int postion, boolean isThumb, int thumbCount) {
            if (recyclerAdapter != null && recyclerAdapter.getDataCount() > postion) {
                Comment comment = recyclerAdapter.getDatas().get(postion);
                comment.is_thumb = isThumb;
                comment.thumb_count = thumbCount;
                recyclerAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onRecyclerItemClick(int i) {
        }

        @Override
        void changeFollow() {
            ToastUtils.show(mContext, videoInfo.owner.is_follow ? "取消关注" : "关注成功");
            videoInfo.owner.is_follow = !videoInfo.owner.is_follow;
            mHeaderView.setIsFollow(videoInfo.owner.is_follow);
        }

        @Override
        void changeAgree() {
            videoInfo.is_thumb = !videoInfo.is_thumb;
            int count = 0;
            if (videoInfo.count != null) {
                if (videoInfo.is_thumb) {
                    videoInfo.count.thumb_up += 1;
                } else {
                    videoInfo.count.thumb_up -= 1;
                }
                count = videoInfo.count.thumb_up;
            } else {
                if (videoInfo.is_thumb) {
                    count = 1;
                } else {
                    count = 0;
                }
            }
            mHeaderView.setIsThumb(videoInfo.is_thumb, count);
        }

        @Override
        void changeFavorite() {
            ToastUtils.show(mContext, videoInfo.is_favorite ? "取消收藏" : "收藏成功");
            videoInfo.is_favorite = !videoInfo.is_favorite;
            mView.setIsFavorite(videoInfo.is_favorite);
        }

        @Override
        void changeComment(boolean isAdd) {
            if (isAdd) {
                videoInfo.count.comment += 1;
            } else {
                videoInfo.count.comment -= 1;
            }
            mView.changeCommentCount(videoInfo.count.comment);
        }

        public void goConsult() {
            super.goConsult(videoInfo.project_id, 0, Constants.COOKIE_SP);
        }

        public void favorite() {
            favorite(!videoInfo.is_favorite);
        }

        public void showDialog() {
            if (videoInfo == null) {
                return;
            }
            ShareDialog sharedDialog = ShareDialog.newInstance(Constants.TOPIC_TYPE_VIDEO, videoInfo.video_id, ShareDialog.TYPE_REPORT, videoInfo.title, videoInfo.desc, videoInfo.logo);
            sharedDialog.show(mView.getMyFragmentManager(), "Shared");
        }
    }
}

